package com.pomelo.kill.controller;


import com.pomelo.kill.common.AjaxResult;
import com.pomelo.kill.exception.KillException;
import com.pomelo.kill.model.Car;
import com.pomelo.kill.service.CarService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    private CarService carService;
    /**
     * 加入购物车操作
     */
    @RequestMapping("/add")
    public AjaxResult addToCar(Integer uid, Integer gid) {
        try {
            Car car = new Car();
            car.setUid(uid);
            car.setGid(gid);
            car.setCreateDate(new Date());
            int i = carService.addToCar(car);
            return i > 0 ? AjaxResult.success() : AjaxResult.failure("加入购物车失败");
        } catch (KillException e) {
            return AjaxResult.failure(e.getMessage());
        }

    }

    /**
     * 查询当前用户的购物车所有信息
     * @param uid 用户id
     * @return 购物车商品 信息
     */
    @RequestMapping("/list")
    public AjaxResult list(Integer uid) {
        List<Car> cars = carService.selectBuUid(uid);
        return cars == null ? AjaxResult.failure("查询失败") : AjaxResult.success(cars);
    }
}
